package userConfig

import (
	"context"
	"devops-super/api/configs/v1"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"
	"github.com/gogf/gf/v2/os/gtime"
)

// GetPageLst 分页获取用户配置列表
func (*ControllerV1) GetPageLst(ctx context.Context, req *v1.UserConfigGetPageLstReq) (res *v1.UserConfigGetPageLstRes, err error) {
	res = &v1.UserConfigGetPageLstRes{}
	res.PageLstRes, err = service.UserConfig().GetPageLst(ctx, req)
	return
}

// Add 新增用户配置
func (*ControllerV1) Add(ctx context.Context, req *v1.UserConfigAddReq) (res *v1.UserConfigAddRes, err error) {
	config := &entity.UserConfig{
		UserId:      req.UserId,
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		CreatedAt:   gtime.Now(),
	}
	err = service.UserConfig().Add(ctx, config)
	return
}

// Upt 更新用户配置
func (*ControllerV1) Upt(ctx context.Context, req *v1.UserConfigUptReq) (res *v1.UserConfigUptRes, err error) {
	config := &entity.UserConfig{
		Id:          int64(req.Id),
		UserId:      req.UserId,
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		UpdatedAt:   gtime.Now(),
	}
	err = service.UserConfig().Upt(ctx, config)
	return
}

// Del 删除用户配置
func (*ControllerV1) Del(ctx context.Context, req *v1.UserConfigDelReq) (res *v1.UserConfigDelRes, err error) {
	err = service.UserConfig().Del(ctx, req.Id)
	return
}

// GetOne 获取单条用户配置
func (*ControllerV1) GetOne(ctx context.Context, req *v1.UserConfigGetOneReq) (res *v1.UserConfigGetOneRes, err error) {
	info, err := service.UserConfig().GetOne(ctx, req.Id)
	if err != nil {
		return
	}
	res = &v1.UserConfigGetOneRes{Info: info}
	return
}

// GetOptions 获取用户配置选项
func (*ControllerV1) GetOptions(ctx context.Context, req *v1.UserConfigGetOptionsReq) (res *v1.UserConfigGetOptionsRes, err error) {
	res, err = service.UserConfig().GetOptions(ctx, req)
	return
}

// GetGroupConfigs 获取配置组
func (*ControllerV1) GetGroupConfigs(ctx context.Context, req *v1.GetGroupConfigsReq) (res *v1.GetGroupConfigsRes, err error) {
	res, err = service.UserConfig().GetGroupConfigs(ctx, req)
	return
}

// SetGroupConfigs 保存配置组
func (*ControllerV1) SetGroupConfigs(ctx context.Context, req *v1.SetGroupConfigsReq) (res *v1.SetGroupConfigsRes, err error) {
	res = &v1.SetGroupConfigsRes{}
	err = service.UserConfig().SetGroupConfigs(ctx, req)
	return
}